import 'package:flutter/material.dart';
import 'package:universally/universally.dart';

class RadioPage extends StatelessWidget {
  const RadioPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      appBarTitleText: 'RadioPage',
      spacing: 20,
      padding: EdgeInsets.all(20),
      children: [
        ValueBuilder<bool>(
          initial: false,
          builder: (_, bool? value, updater) {
            return _Radio(value: value!, onChanged: updater);
          },
        ),
      ],
    );
  }
}

class _Radio extends StatefulWidget {
  const _Radio({required this.value, required this.onChanged});

  final bool value;

  final ValueCallback<bool> onChanged;

  @override
  State<_Radio> createState() => _RadioState();
}

class _RadioState extends State<_Radio> with SingleTickerProviderStateMixin {
  late AnimationController controller;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(vsync: this, duration: Duration(milliseconds: 200));
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: controller,
      builder: (_, __) {
        return Universal(
          onTap: () {
            widget.onChanged(!widget.value);
          },
          width: 14,
          height: 14,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            border: Border.all(color: widget.value ? Colors.blue : Colors.grey, width: controller.value * 3.5 + 1.5),
          ),
        );
      },
    );
  }

  @override
  void didUpdateWidget(covariant _Radio oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.value != oldWidget.value) {
      if (widget.value) {
        controller.forward();
      } else {
        controller.reverse();
      }
    }
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}

